<?php
	class Manage_message extends CI_Controller{	
		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_message");
			$this->load->model("apanel/db_news");
			$this->load->library("pagination");
		}
		
		public function index(){
			//== template
			redirect('manage_message/message_list');
		}
		
		public function message_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			//list($item,$num,$page,$total) = $this->listall();
			//$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			$scripts = '
			<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
			$(\'#table1\').dataTable( {
			"bJQueryUI": true,
			"bProcessing": true,
			"bServerSide": true,
			"sAjaxSource": "'.base_url().'manage_message/datalist",
			"iDisplayStart"  : 0,
			"sPaginationType": "full_numbers",
			"aaSorting": [[ 0, "desc" ]],
			"aoColumnDefs": [
			{ "bSortable": false, "aTargets": [ 0 ] },
			{ "bSortable": false, "aTargets": [ 3 ] },
			{ "bSortable": false, "aTargets": [ 4 ] },
			],
				"fnServerData": function(sSource, aoData, fnCallback)
			{
			$.ajax(
			{
			\'dataType\': \'json\',
			\'type\'  : \'POST\',
			\'url\'    : sSource,
			\'data\'  : aoData,
			\'success\' : fnCallback
			}
			);
			}
			} );
			} );
			</script>';
			//== template
		
			$this->cismarty->assign("scripts",$scripts);
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(11));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_message/message_item_delAll"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/sentmessage/message_list.html');
		}
		
			public function datalist(){
			$output = $this->db_message->datalist();
			echo json_encode($output);
		}
		
		public function message_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				//$this->form_validation->set_rules("status","Status","required");
				$this->form_validation->set_rules("title","Title","required");
				$this->form_validation->set_rules("detail","Detail","required");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("type","Type",'required');
				
			
				if($this->form_validation->run() == FALSE){
					
					//set error
					
					$this->cismarty->assign("errorstatus",form_error('status'));
					$this->cismarty->assign("errortitle",form_error('title'));
					$this->cismarty->assign("errordetail",form_error('detail'));
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					$this->cismarty->assign("errortype",form_error('type'));
				}else{
					
						$data = array(
				
								"title"=>$this->input->post('title'),
								"detail"=>$this->input->post('detail'),
								"type"=>$this->input->post('type'),
								"customer_pkid"=>$this->input->post('customer'),
								"sentby"=>$this->session->userdata("sess_username"),
								"sentdate"=>date('Y-m-d H:i:s'),
							
						);
						
							$res = $this->db->insert('mobile_message',$data);
							$answer_id = $this->db->insert_id();
							
							
						
							$a = $this->input->post("customer");
							$this->db->select("pkid");
							$this->db->from("mobile_member");
							$this->db->where("customer_pkid",$a);
							
			
							$res3 = $this->db->get()->result_array();
								foreach ($res3 as $P){
				
									$tmp['pkid']			=$P['pkid'];
			
									$data1 = array(
					
										"customer_pkid"=>$this->input->post('customer'),
										"message_pkid"=>$answer_id,
										"member_pkid"=>$P['pkid'],
									);
							
									$res1 = $this->db->insert('mobile_mes_natificate',$data1);
		
								}
						if($res)
							 redirect('manage_message/message_list/success');
						else 
							redirect('manage_message/message_list/fail');
					}
				
			}//end post
			
			//== template
			$this->cismarty->assign("customer",$this->db_news->customer_list());
			$this->cismarty->assign("str_type",$this->db_message->type());
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu());
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart('manage_message/message_add'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/sentmessage/message_add.html');
			
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////////////Edit
		/*public function message_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			$class ="";
			if($this->input->post('bntSubmit') != null){
					//$this->form_validation->set_rules("status","Status","required");
				$this->form_validation->set_rules("title","Title","required");
				$this->form_validation->set_rules("detail","Detail","required");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("type","Type",'required');
			
				if($this->form_validation->run() == FALSE){
					//set error
					
			
					$this->cismarty->assign("errortitle",form_error('title'));
					$this->cismarty->assign("errordetail",form_error('detail'));
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					$this->cismarty->assign("errortype",form_error('type'));
				}
					$error=false;
					if(!$error){
						$data = array(
								"title"=>$this->input->post('title'),
								"detail"=>$this->input->post('detail'),
								"type"=>$this->input->post('type'),
								"sentby"=>$this->session->userdata("sess_username"),
								"sentdate"=>date('Y-m-d H:i:s'),
								"customer_pkid"=>$this->input->post('customer'),
								
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('mobile_message',$data);
						
						$data1 = array(

								"customer_pkid"=>$this->input->post('customer'),
								
						);
						
						$this->db->where("message_pkid",$id);
						$res1 = $this->db->update('mobile_mes_natificate',$data1);
						
						if($res&&$res1) redirect('manage_message/message_list/success');
						else  redirect('manage_message/message_list/fail');
					}
				
			}//end post
			else{
				$this->db->where("t1.pkid",$id);
				$this->db->where("t2.message_pkid",$id);
				$row = $this->db->get("mobile_message t1,mobile_mes_natificate t2")->row();
				$this->cismarty->assign("title",$row->title);
				$this->cismarty->assign("detail",$row->detail);
				$type			= $row->type;
				$group 			= $row->customer_pkid;
				if($row->status == 1) $this->cismarty->assign("sa",'checked="checked"');
				else $this->cismarty->assign("sd",'checked="checked"');
		
				
			}
		
			//== template
			$this->cismarty->assign("customer",$this->db_news->customer_list($group));
			$this->cismarty->assign("str_type",$this->db_message->type($type));
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(4));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_message/message_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/sentmessage/message_edit.html');
			
		}*/
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////Delete
	public function message_item_del($id){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($id != ""){
 			$this->db->where('pkid',$id);
 			$res = $this->db->delete("mobile_message");
 			if($res){
			$this->db->where('message_pkid',$id);
 			$this->db->delete("mobile_mes_natificate");
			 redirect('manage_message/message_list/success');
			 }
 		}
 	}
 	/////////////////////////////////////////////////////////////////////////////
 	public function message_item_delAll(){
 		$check = $this->input->post('check');
 		if($check != ""){
 			foreach ($check as $index){
 				$this->db->where('pkid',$index);
 				$res = $this->db->delete("mobile_message");
				if($res){
						$this->db->where('message_pkid',$index);
 						$this->db->delete("mobile_mes_natificate");
					}
				
 			}
 		}
 		redirect('manage_message/message_list/success');
 	}
		
		
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	

	
	}
?>